package com.pic4493.adapter;

import java.util.ArrayList;

import com.pic4493.app.R;
import com.pic4493.entities.EMessage;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class AdpList_Message extends BaseAdapter
{
   private OnClickEvent mEvent;
   private LayoutInflater mInflater;
   private ArrayList<EMessage> mList;

   public AdpList_Message(Context context, ArrayList<EMessage> list, OnClickEvent event)
   {
	  mInflater = LayoutInflater.from(context);
	  mList = list;
	  mEvent = event;
   }

   public void removeItem(int positon)
   {
	  try
	  {
		 mList.remove(mList.get(positon));
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "AdpList_Message_removeItem(positon) " + e);
	  }
   }

   @Override
   public int getCount()
   {
	  return mList == null ? 0 : mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int position)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent)
   {
	  ViewHolder holder = null;
	  if (convertView == null)
	  {
		 convertView = mInflater.inflate(R.layout.uimessage_item, parent, false);
		 holder = new ViewHolder();
		 holder.mTxtTitleF = (TextView) convertView.findViewById(R.id.uimessage_item_txt_title);
		 holder.mTxtContentF = (TextView) convertView.findViewById(R.id.uimessage_item_txt_content);
		 holder.mTxtTimeF = (TextView) convertView.findViewById(R.id.uimessage_item_txt_time);
		 holder.mBtnReplyB = (Button) convertView.findViewById(R.id.uimessage_item_btn_reply);
		 holder.mBtnDelB = (Button) convertView.findViewById(R.id.uimessage_item_btn_del);
		 convertView.setTag(holder);
	  }
	  else
	  {
		 holder = (ViewHolder) convertView.getTag();
	  }

	  holder.mTxtTitleF.setText(mList.get(position).getTitle());
	  holder.mTxtContentF.setText(mList.get(position).getContent());
	  holder.mTxtTimeF.setText(mList.get(position).getTime());
	  holder.mBtnDelB.setTag(position);
	  holder.mBtnReplyB.setTag(position);
	  holder.mBtnReplyB.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			mEvent.Reply(Integer.valueOf(v.getTag().toString()), v);
		 }
	  });
	  holder.mBtnDelB.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			mEvent.Delete(Integer.valueOf(v.getTag().toString()), v);
		 }
	  });
	  return convertView;
   }

   // 后缀F是前面的,B是后面的
   static class ViewHolder
   {
	  TextView mTxtTitleF, mTxtContentF, mTxtTimeF;
	  Button mBtnReplyB, mBtnDelB;
   }

   public interface OnClickEvent
   {
	  public void Reply(int position, View v);

	  public void Delete(int position, View v);
   }

}
